﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;

namespace WebApplication2.student
{
    public partial class 学生选课 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["xh"] != null)
            {
                if (!IsPostBack)
                {
                    bind();
                }
            }


        }
        private void bind()
        {
            string xh = "";
            xh = Session["xh"].ToString();
            string sql = "select kcdm,kcmc,dm,xm,xf,dd,(select COUNT(*) from kc_xs where xh='"+xh+ "' and kcdm=kc.kcdm) as flag,kclx from kc,ls where kc.id_ls=ls.id";
            Database db = new Database();
            DataTable dt = db.SelectSQL(sql);

            DataList1.DataSource = dt;
            DataList1.DataBind();
        }

        private void delete(string xh)
        {
            string sql = "delete from kc_xs where xh='"+xh+"'";
            Database db = new Database();
            db.ExecSQL(sql);
        }

        private void insert(string xh,string kcdm)
        {
            string sql = "insert into kc_xs(xh,kcdm) values('"+xh+ "','" + kcdm + "')";
            Database db = new Database();
            db.ExecSQL(sql);
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string xh = Session["xh"].ToString();
            delete(xh);

            foreach(DataListItem item in DataList1.Items)
            {
                CheckBox CheckBox1 = (CheckBox)item.FindControl("CheckBox1");//在当前的命名容器中搜索带指定 id
                if (CheckBox1.Checked)
                {
                    Label lbl_kcdm = (Label)item.FindControl("lbl_kcdm");
                    insert(xh, lbl_kcdm.Text);
                }
            }
            bind();
            Response.Write("<SCRIPT LANGUAGE=\"JavaScript\">" + "alert('保存成功!');</SCRIPT>");
        }

        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                string kclx = ((DataRowView)e.Item.DataItem).Row["kclx"].ToString().Trim();
                if (kclx == "必选课")
                {
                    Label lbl_kcdm = (Label)e.Item.FindControl("lbl_kcmc");
                    lbl_kcdm.ForeColor = Color.Red;
                }
                else
                {
                    Label lbl_kcdm = (Label)e.Item.FindControl("lbl_kcmc");
                    lbl_kcdm.ForeColor = Color.Black;
                }
                int xf = Convert.ToInt32(((DataRowView)e.Item.DataItem).Row["xf"].ToString().Trim());
                if (xf > 5)
                {
                    Label lbl_kcdm = (Label)e.Item.FindControl("lbl_kcdm");
                    lbl_kcdm.ForeColor = Color.Red;
                }
                else
                {
                    Label lbl_kcdm = (Label)e.Item.FindControl("lbl_kcdm");
                    lbl_kcdm.ForeColor = Color.Black;
                }
            }
        }
    }
}